package cn.com.nes.site.entity.vo;

import cn.com.nes.site.entity.UserInfo;
import cn.com.nes.site.entity.bo.BaseBO;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * 项目基础参数模型
 */
@Slf4j
@ApiModel
@Data
public abstract class BaseVO {

    @ApiModelProperty(value = "操作用户ID", hidden = true)
    @TableField(exist = false)
    private Long operateUserId;

    @ApiModelProperty(value = "操作用户名称", hidden = true)
    @TableField(exist = false)
    private String operateUserName;

    @ApiModelProperty(value = "操作用户昵称", hidden = true)
    @TableField(exist = false)
    private String operateNickName;

    @ApiModelProperty(value = "操作用户的公司ID", hidden = true)
    @TableField(exist = false)
    private String operateUserCompanyId;

    @ApiModelProperty(value = "操作用户的长效管护公司ID", hidden = true)
    @TableField(exist = false)
    private String operateUserCallCenterCompanyId;

    @ApiModelProperty(value = "操作用户的运营平台公司ID", hidden = true)
    @TableField(exist = false)
    private String operateUserOpeCompanyId;

    @ApiModelProperty(value = "操作用户公司类型", hidden = true)
    @TableField(exist = false)
    private String operateUserCompanyType;

    @ApiModelProperty(value = "操作人手机号", hidden = true)
    @TableField(exist = false)
    private String operateUserPhone;

    @ApiModelProperty(value = "操作时间", hidden = true)
    @TableField(exist = false)
    private Date operateDate = new Date();

    @ApiModelProperty(value = "操作时间戳", hidden = true)
    @TableField(exist = false)
    private Long operateTimeStamp = System.currentTimeMillis();

    @ApiModelProperty(value = "操作平台", hidden = true)
    @TableField(exist = false)
    private String operatePlatform;

    @ApiModelProperty(value = "用户信息", hidden = true)
    @TableField(exist = false)
    private UserInfo userInfo;

    @ApiModelProperty(value = "日志追踪标识", hidden = true)
    @TableField(exist = false)
    private String traceId;

    @ApiModelProperty(value = "操作用户行政区划代码集合", hidden = true)
    @TableField(exist = false)
    private Set<String> operateUserAreaCodeSet = new HashSet<>();

    @JsonIgnore
    public <T extends BaseVO> void settingOperateInfo(T operater) {
        this.setOperateUserId(operater.getOperateUserId());
        this.setOperateUserName(operater.getOperateUserName());
        this.setOperateUserCompanyId(operater.getOperateUserCompanyId());
        this.setOperatePlatform(operater.getOperatePlatform());
        this.setOperateDate(operater.getOperateDate());
        this.setOperateTimeStamp(operater.getOperateTimeStamp());
        this.setOperateUserPhone(operater.getOperateUserPhone());
        this.setUserInfo(operater.getUserInfo());
    }

    public <T extends BaseBO> T convertToBo(T  t) {
        BeanUtils.copyProperties(this, t);
        return t;
    }

    public <T extends BaseBO> T convertToBo(Class<T> clazz) {
        return copyOperateInfo(this, clazz);
    }

    private  <T extends BaseBO, S extends BaseVO> T copyOperateInfo(S s, Class<T> clazz) {
        T target = null;
        try {
            target = clazz.newInstance();
        } catch (Exception e) {
            log.warn("copyOperateInfo", e);
        }
        assert target != null;
        BeanUtils.copyProperties(s, target);
        return target;
    }

    public interface Save {}

    public interface Update {}
}
